package org.sippr.controller;

import org.sippr.pojo.Result;
import org.sippr.pojo.SdCheckPoints;
import org.sippr.pojo.SdLorra;
import org.sippr.pojo.SdStyle;
import org.sippr.service.SdCheckPointsService;
import org.sippr.service.SdLorraService;
import org.sippr.service.SdStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/sd/style")
@RestController
public class SdStyleController {
    @Autowired
    private SdStyleService sdStyleService;

    @Autowired
    private SdCheckPointsService sdCheckPointsService;

    @Autowired
    private SdLorraService sdLorraService;

    //新增风格，仅存在名字
    @PostMapping("/add")
    public Result<String> add(@RequestBody @DefaultValue("{\"parentId\":-1}") @Validated(SdStyle.Add.class) SdStyle value) {
        List<SdStyle> sdStyles = sdStyleService.findByName(value.getName());
        if (!sdStyles.isEmpty())
            return Result.error("改风格名称已存在！");
        sdStyleService.add(value);
        return Result.success();
    }

    //选择check points
    @PutMapping("/cp")
    public Result<String> cp(@RequestBody SdStyle value) {
        sdStyleService.update(value);
        return Result.success();
    }

    //删除check points
    @DeleteMapping("/cp")
    public Result<String> deleteCp(Integer id) {

        System.out.println(id);
        sdStyleService.deleteCp(id);
        return Result.success();
    }

    //选择lorra
    @PutMapping("/lorra")
    public Result<String> lorra(@RequestBody SdStyle value) {

        sdStyleService.update(value);
        return Result.success();
    }

    //删除lorra
    @DeleteMapping("/lorra")
    public Result<String> deleteLorra(Integer id) {
        sdStyleService.deleteLorra(id);
        return Result.success();
    }

    //删除风格
    @DeleteMapping()
    public Result<String> delete(Integer id) {
        sdStyleService.delete(id);
        return Result.success();
    }

    //获取风格列表，树列表
    @GetMapping("/tree")
    public Result tree() {
        List<SdStyle> allSdStyles = sdStyleService.list();
        if (allSdStyles.isEmpty()) {
            return Result.error("未获取到数据");
        }

        Map<Integer, SdStyle> sdStyleMapMap = new HashMap<>();
        for (SdStyle item : allSdStyles) {
            sdStyleMapMap.put(item.getId(), item);
        }

        List<SdStyle> sdStyleTree = new ArrayList<>();
        SdStyle root = new SdStyle();
        root.setId(-1);
        root.setName("根目录");
        sdStyleTree.add(root);
        for (SdStyle item : allSdStyles) {
            Integer parentId = item.getParentId();
            if (parentId == -1) {
                root.getChildren().add(item);
            } else {
                SdStyle parent = sdStyleMapMap.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(item);
                }
            }
        }
        return Result.success(sdStyleTree);
    }


    //获取风格列表，供前端使用，命名方式：父节点（子节点），以列表形式展现，非树状结构
    @GetMapping("/list")
    public Result list() {
        List<SdStyle> allSdStyles = sdStyleService.list();
        if (allSdStyles.isEmpty()) {
            return Result.error("未获取到数据");
        }

        Map<Integer, SdStyle> sdStyleMapMap = new HashMap<>();
        for (SdStyle item : allSdStyles) {
            sdStyleMapMap.put(item.getId(), item);
        }

        List<SdStyle> list = new ArrayList<>();

        for (SdStyle item : allSdStyles) {
            Integer parentId = item.getParentId();
            if (parentId == -1) {
                if (!(item.getCpId() == null || item.getLorraId() == null)) {
                    list.add(item);
                }


            } else {
                SdStyle parent = sdStyleMapMap.get(parentId);
                if (parent != null) {
                    if (!(item.getCpId() == null || item.getLorraId() == null)) {
                        item.setName(parent.getName() + "(" + item.getName() + ")");
                        list.add(item);
                    }
                }
            }
        }
        return Result.success(list);
    }

    //获取指定风格对应得check points 和lorra
    @GetMapping("/models")
    public Result models(Integer id) {
        SdStyle sdStyle = sdStyleService.get(id);
        if (sdStyle == null)
            Result.error("id不正确！");
        Integer cpId = sdStyle.getCpId();
        if (cpId != null) {
            SdCheckPoints sdCheckPoints = sdCheckPointsService.get(sdStyle.getCpId());
            sdCheckPoints.setImg("/file/check_points/" + sdCheckPoints.getImg());
            sdStyle.setCheckPoints(sdCheckPoints);
        }
        Integer lorraId = sdStyle.getLorraId();
        if (lorraId != null) {
            SdLorra sdLorra = sdLorraService.get(sdStyle.getLorraId());
            sdLorra.setImg("/file/lorra/" + sdLorra.getImg());
            sdStyle.setLorra(sdLorra);
        }


        return Result.success(sdStyle);
    }

}
